package com.yunkaweilai.android.base;


import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.orhanobut.logger.Logger;
import com.yunkaweilai.android.R;
import com.yunkaweilai.android.utils.TitleBuilder;
import com.yunkaweilai.android.utils.refresh.BGAStickinessRefreshViewHolder1;
import com.yunkaweilai.android.view.MyToast;

import cn.bingoogolapple.refreshlayout.BGARefreshLayout;


public abstract class BaseFragment extends Fragment {

    protected final String TAG = this.getClass().getSimpleName();
    protected BaseActivity mAc;
    public View mRootView;
    private int mTabPos = 0;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.mAc = (BaseActivity) getActivity();
    }

    public void setTabPos(int mTabPos) {
        this.mTabPos = mTabPos;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mRootView = inflater.inflate(getContentViewId(), container, false);
        return mRootView;
    }

    /**
     * 设置Fragment布局
     *
     * @return
     */
    public abstract int getContentViewId();

    /**
     * 左侧有返回键的标题栏
     * <p>如果在此基础上还要加其他内容,比如右侧有文字按钮,可以获取该方法返回值继续设置其他内容
     *
     * @param title 标题
     */
    protected TitleBuilder initBackAndTitle(String title) {
        return new TitleBuilder(mRootView)
                .setTitleText(title)
                .setLeftImage(R.mipmap.ic_go_back)
                .setLeftOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mAc.finish();
                    }
                });
    }

    /**
     * 跳转页面,无extra简易型
     *
     * @param tarActivity 目标页面
     */
    public void intent2Activity(Class<? extends Activity> tarActivity) {
        Intent intent = new Intent(mAc, tarActivity);
        startActivity(intent);
    }

    /**
     * 携带数据的页面跳转
     *
     * @param clz
     * @param bundle
     */
    public void intent2Activity(Class<? extends Activity> clz, Bundle bundle) {
        Intent intent = new Intent();
        intent.setClass(mAc, clz);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivity(intent);
    }

    /**
     * 含有Bundle通过Class打开编辑界面
     *
     * @param cls
     * @param bundle
     * @param requestCode
     */
    public void startActivityForResult(Class<?> cls, Bundle bundle,
                                       int requestCode) {
        Intent intent = new Intent();
        intent.setClass(mAc, cls);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivityForResult(intent, requestCode);
    }

    /**
     * 打印Toast
     *
     * @param msg
     */
    protected void showToast(String msg) {
        MyToast.showShortToast(mAc,msg);
    }

    /**
     * 打印Log
     *
     * @param msg
     */
    protected void showLog(String msg) {
        Logger.t(TAG).e(msg);
    }

    protected void showProgressDialog() {
        mAc.showProgressDialog();
    }

    protected void dismissProgressDialog() {
        mAc.dismissProgressDialog();
    }

    /**
     * 初始化下拉刷新控件，请在onCreateView方法中调用
     *
     * @param refreshLayout 控件
     * @param delegate      实现接口的Activity或Fragment
     * @param isLoading     是否具有上拉加载功能
     */
    public void initRefreshLayout(BGARefreshLayout refreshLayout, BGARefreshLayout.BGARefreshLayoutDelegate delegate, boolean isLoading) {
//        // 为BGARefreshLayout 设置代理
//        refreshLayout.setDelegate(delegate);
//        // 设置下拉刷新和上拉加载更多的风格     参数1：应用程序上下文，参数2：是否具有上拉加载更多功能
//        BGARefreshViewHolder refreshViewHolder = new BGANormalRefreshViewHolder(mAc, isLoading);
//
//        // 设置下拉刷新和上拉加载更多的风格
//        refreshLayout.setRefreshViewHolder(refreshViewHolder);
//        refreshLayout.setIsShowLoadingMoreView(true);

                // 为BGARefreshLayout 设置代理
        refreshLayout.setDelegate(delegate);
        // 设置下拉刷新和上拉加载更多的风格     参数1：应用程序上下文，参数2：是否具有上拉加载更多功能
        BGAStickinessRefreshViewHolder1 bgaStickinessRefreshViewHolder = new BGAStickinessRefreshViewHolder1(mAc,isLoading);
//        moocStyleRefreshViewHolder.setLoadMoreBackgroundColorRes(R.color.custom_imoocstyle);
        bgaStickinessRefreshViewHolder.setRotateImage(R.mipmap.bga_refresh_stickiness);
        bgaStickinessRefreshViewHolder.setStickinessColor(R.color.wjx_title_bg_next);
        // 设置下拉刷新和上拉加载更多的风格
        refreshLayout.setRefreshViewHolder(bgaStickinessRefreshViewHolder);
        refreshLayout.setIsShowLoadingMoreView(true);
    }

}
